package solution._0150._0153;

/**
 * Created by chen_snow
 * Date: 2021/7/9 0:56
 * Description:
 */
public class Solution {
    public static void main(String[] args) {
        findMin(new int[]{4, 5, 1, 2, 3});
    }

    public static int findMin(int[] nums) {
        if (nums[0] <= nums[nums.length - 1]) {
            return nums[0];
        }

        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        int start = 0;
        int end = nums.length - 1;

        while (start <= end) {
            int middle = start + (end - start) / 2;

            if (nums[middle] > nums[middle - 1]) {
                return nums[middle + 1];
            }

            if (nums[middle] > nums[0]) {
                start = middle;
            } else {
                end = middle;
            }
        }
        return 0;
    }
}
